import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.*;

class Tra extends JFrame implements ActionListener {
    Conn c = new Conn();
    JTextArea show1 = new JTextArea();
    JPanel p1, p2;
    private JTextArea show;
    private String[] text = {
            "查询单词", "添加词典", "删除词典", "修改词典", "查询所有"
    };
    private JButton[] Button = new JButton[text.length];
    public Tra() {
        p1 = new JPanel();
        p2 = new JPanel();
        show = new JTextArea();
        p1.setSize(1000, 40);
        p2.setSize(1000, 230);
    }

    public void init() {//初始化
        this.setTitle("中英互译查询软件");
        this.setBounds(200, 200, 1000, 270);
        this.setLocation(100, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.add(p1, BorderLayout.NORTH);
        this.add(p2, BorderLayout.CENTER);
        this.setVisible(true);

        JLabel label = new JLabel("输入你要查询的单词");
        p1.add(label);
        label.setPreferredSize(new Dimension(180, 40));
        show.setPreferredSize(new Dimension(200, 30));
        p1.add(show);


        for (int i = 0; i < text.length; i++) {
            Button[i] = new JButton(text[i] + " ");
            p1.add(Button[i]);
            show1.setPreferredSize(new Dimension(600, 200));
            p2.add(show1);

            // 查询单词按钮的ActionListener
            if (i == 0) {
                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        if (!(show.getText().equals(""))) {
                            String s1 = show.getText();
                            String word = null;
                            try {
                                word = c.select(s1);
                                show1.setText(word);
                            } catch (SQLException ex) {
                                throw new RuntimeException(ex);
                            }

                        }
                    }
                });
            }


            // 添加词典按钮的ActionListener
            else if (i == 1) {

                Button[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JTextField field1 = new JTextField();
                        JTextField field2 = new JTextField();
                        JTextField field3 = new JTextField();

                        JPanel panel = new JPanel();
                        panel.setLayout(new GridLayout(3, 2));
                        panel.add(new JLabel("输入词语的id"));
                        panel.add(field1);
                        panel.add(new JLabel("请输入要增添的中文词语"));
                        panel.add(field2);
                        panel.add(new JLabel("请输入要增添的英语单词"));
                        panel.add(field3);

                        int result = JOptionPane.showConfirmDialog(null, panel, "增添词库", JOptionPane.OK_CANCEL_OPTION);

                        if (result == JOptionPane.OK_OPTION) {

                            String w_id = field1.getText();
                            String Chineseword = field2.getText();
                            String English = field3.getText();
                            try {
                                show1.setText(c.add(w_id, Chineseword, English));
                            } catch (SQLException ex) {
                                throw new RuntimeException(ex);
                            }

                        } else {
                            System.out.println("添加失败");

                        }
                    }
                });
            }


            // 删除词典按钮的ActionListener
            else if (i == 2) {
                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if (!(show.getText().equals(""))) {
                            String s3 = show.getText();
                            String result = null;
                            try {
                                result = c.del(s3);
                                show1.setText(result);
                            } catch (SQLException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }
                });

            }
            // 修改词典按钮的ActionListener
            else if (i == 3) {
                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JTextField field1 = new JTextField();
                        JTextField field2 = new JTextField();
                        JTextField field3 = new JTextField();
                        JTextField field4 = new JTextField();

                        JPanel panel = new JPanel();
                        panel.setLayout(new GridLayout(4, 2));
                        panel.add(new JLabel("修改的中文"));
                        panel.add(field1);
                        panel.add(new JLabel("修改的英文"));
                        panel.add(field2);
                        panel.add(new JLabel("原来的中文"));
                        panel.add(field3);
                        panel.add(new JLabel("原来的英文"));
                        panel.add(field4);

                        int result = JOptionPane.showConfirmDialog(null, panel, "修改词库", JOptionPane.OK_CANCEL_OPTION);
                        if (result == JOptionPane.OK_OPTION) {
                            String s1 = field1.getText();
                            String s2 = field2.getText();
                            String Chineseword = field3.getText();
                            String English = field4.getText();
                            try {
                                c.update(s1, s2, Chineseword, English); // 调用更新词典的方法
                            } catch (SQLException ex) {
                                throw new RuntimeException(ex);
                            }
                        } else {
                            System.out.println("修改操作被取消");
                        }
                    }
                });
            }


            // 查询所有词典按钮的ActionListener
            else if (i == 4) {

                Button[i].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        StringBuilder sb = null;

                        try {
                            sb = c.selectAll();
                            show1.setText(sb.toString());
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                });

            }
            }
        }
    public static void main(String[] args) {

        new Tra().init();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        }
    }




